package com.example.admin.goodnews.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.admin.goodnews.R;
import com.example.admin.goodnews.entity.DistrictNewsDataEntity;
import com.example.admin.goodnews.entity.GoodNewsDataEntity;
import com.example.admin.goodnews.interfaces.RecyclerViewItemClickListener;
import com.example.admin.goodnews.interfaces.RecyclerViewItemLongClickListener;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * 国内焦点适配器
 * Created by admin on 2016/10/8.
 */

public class DistrictNewsDataAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<DistrictNewsDataEntity.ShowapiResBodyBean.PagebeanBean.ContentlistBean> datalists;
    int type = 0;

    private RecyclerViewItemClickListener recyclerViewItemClickListener;
    private RecyclerViewItemLongClickListener recyclerViewItemLongClickListener;

    public void setRecyclerViewItemClickListener(RecyclerViewItemClickListener recyclerViewItemClickListener) {
        this.recyclerViewItemClickListener = recyclerViewItemClickListener;
    }

    public void setRecyclerViewItemLongClickListener(RecyclerViewItemLongClickListener recyclerViewItemLongClickListener) {
        this.recyclerViewItemLongClickListener = recyclerViewItemLongClickListener;
    }

    public DistrictNewsDataAdapter(Context context, List<DistrictNewsDataEntity.ShowapiResBodyBean.PagebeanBean.ContentlistBean> datalists) {
        this.context = context;
        this.datalists = datalists;
    }

    @Override
    public int getItemViewType(int position) {
        if (datalists.get(position).getImageurls().size() > 0) {
            type = 1;// 表示有图
        } else if (datalists.get(position).getImageurls().size() == 0) {
            type = 0;// 表示无图
        }
        return type;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 无图
        if (type == 0) {
            View view_nopic = View.inflate(context, R.layout.item_districtnews_nopic, null);
            ViewHolder_nopic holder_nopic = new ViewHolder_nopic(view_nopic);
            return holder_nopic;
            // 有图
        } else if (type == 1) {
            View view_withpic = View.inflate(context, R.layout.item_districtnews_withpic, null);
            ViewHolder_withpic holder_withpic = new ViewHolder_withpic(view_withpic);
            return holder_withpic;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        // 有图
        if (holder instanceof ViewHolder_withpic) {
            ((ViewHolder_withpic) holder).tv_districtnews_title_withpic.setText(datalists.get(position).getTitle());
            ((ViewHolder_withpic) holder).tv_districtnews_areaname_withpic.setText(datalists.get(position).getAreaName());
            ((ViewHolder_withpic) holder).tv_districtnews_source_withpic.setText(datalists.get(position).getSource());
            ((ViewHolder_withpic) holder).tv_districtnews_time_withpic.setText(datalists.get(position).getPubDate().substring(0,10));
            Picasso.with(context).load(datalists.get(position).getImageurls().get(0).getUrl()).into(((ViewHolder_withpic) holder).iv_newsimage_districtnews_withpic);
            // 无图
        } else if (holder instanceof ViewHolder_nopic) {
            ((ViewHolder_nopic) holder).tv_districtnews_title_nopic.setText(datalists.get(position).getTitle());
            ((ViewHolder_nopic) holder).tv_districtnews_areaname_nopic.setText(datalists.get(position).getAreaName());
            ((ViewHolder_nopic) holder).tv_districtnews_source_nopic.setText(datalists.get(position).getSource());
            ((ViewHolder_nopic) holder).tv_districtnews_time_nopic.setText(datalists.get(position).getPubDate().substring(0,10));
        }
        if (recyclerViewItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int clickposition = holder.getLayoutPosition() - 1;
                    recyclerViewItemClickListener.RecyclerViewItemClickListener(clickposition);
                }
            });
        }
        if (recyclerViewItemLongClickListener != null) {
            holder.itemView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_UP) {
                        int clickposition = holder.getLayoutPosition() - 1;
                        recyclerViewItemLongClickListener.RecyclerViewItemLongClickListener(clickposition);
                    }
                    return true;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return datalists.size();

    }

    // 带图的item的ViewHolder
    public class ViewHolder_withpic extends RecyclerView.ViewHolder {
        private TextView tv_districtnews_title_withpic;// 标题
        private TextView tv_districtnews_areaname_withpic;// 省份
        private TextView tv_districtnews_source_withpic;// 来源
        private TextView tv_districtnews_time_withpic;// 时间
        private ImageView iv_newsimage_districtnews_withpic;// 图片

        public ViewHolder_withpic(View itemView) {
            super(itemView);
            tv_districtnews_title_withpic = (TextView) itemView.findViewById(R.id.tv_districtnews_title_withpic); // 标题
            tv_districtnews_areaname_withpic = (TextView) itemView.findViewById(R.id.tv_districtnews_areaname_withpic); // 省份
            tv_districtnews_source_withpic = (TextView) itemView.findViewById(R.id.tv_districtnews_source_withpic); // 来源
            tv_districtnews_time_withpic = (TextView) itemView.findViewById(R.id.tv_districtnews_time_withpic); // 时间
            iv_newsimage_districtnews_withpic = (ImageView) itemView.findViewById(R.id.iv_newsimage_districtnews_withpic); // 图片
        }
    }

    // 无图的item的ViewHolder
    public class ViewHolder_nopic extends RecyclerView.ViewHolder {
        private TextView tv_districtnews_title_nopic;// 标题
        private TextView tv_districtnews_areaname_nopic;// 省份
        private TextView tv_districtnews_source_nopic;// 来源
        private TextView tv_districtnews_time_nopic;// 时间

        public ViewHolder_nopic(View itemView) {
            super(itemView);
            tv_districtnews_title_nopic = (TextView) itemView.findViewById(R.id.tv_districtnews_title_nopic); // 标题
            tv_districtnews_areaname_nopic = (TextView) itemView.findViewById(R.id.tv_districtnews_areaname_nopic); // 频道
            tv_districtnews_source_nopic = (TextView) itemView.findViewById(R.id.tv_districtnews_source_nopic); // 来源
            tv_districtnews_time_nopic = (TextView) itemView.findViewById(R.id.tv_districtnews_time_nopic); // 时间
        }
    }
}
